package com.lbd.media.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lbd.base.service.BaseServiceImpl;
import com.lbd.commons.exception.ControllerException;
import com.lbd.media.bean.FaqType;
import com.lbd.media.dao.FaqRepository;
import com.lbd.media.dao.FaqTypeRepository;
import com.lbd.media.service.FaqTypeService;


@Service
@Transactional
public class FaqTypeServiceImpl extends BaseServiceImpl<FaqType, FaqTypeRepository> implements FaqTypeService {
    private final Logger log = Logger.getLogger(this.getClass());
    @Autowired
    FaqTypeRepository faqtypeRepository;
    @Autowired
    FaqRepository faqRepository;

    @Override
    public Specification<FaqType> getSpecification(FaqType faqtype) {
        return null;
    }

    /**
     * 构建查询语句和参数
     *
     * @param faqtype
     * @param parameter
     * @return
     */
    @Override
    public String buildJpql(FaqType faqtype, Map<String, Object> parameter) {
        StringBuilder jpqlBuilder = new StringBuilder("  from  FaqType t   ");
        if (faqtype != null) {
            if (StringUtils.isNotBlank(faqtype.getName())) {
                jpqlBuilder.append(" and  t.name like :name");
                parameter.put("name", "%" + faqtype.getName() + "%");
            }
            if (faqtype.getParent() != null && StringUtils.isNotBlank(faqtype.getParent().getId())) {
                jpqlBuilder.append(" and  t.parent.id = :parentId");
                parameter.put("parentId", faqtype.getParent().getId());
            } else {
                //过滤问题栏目--即顶层分类
                jpqlBuilder.append(" and  t.parent.id is not null ");
            }
        }

        jpqlBuilder.append(" order by orderBy asc ");
        return jpqlBuilder.toString();
    }


    @Override
    public List<FaqType> findAllParents() {
        return em.createQuery(" from FaqType where parent is null").getResultList();
    }

    @Override
    public List<FaqType> findAllTypes() {
        return em.createQuery(" from FaqType where parent is not null order by orderBy asc").getResultList();
    }

    @Override
    public void save(FaqType faqType) {
        //校验
        if (StringUtils.isNotBlank(faqType.getCode())) {
            long count = faqtypeRepository.countByCode(faqType.getCode());
            if (count > 0) {
                throw new ControllerException("编码已存在");
            }
        } else {
            throw new ControllerException("请输入编码");
        }
        super.save(faqType);
    }

    @Override
    public void update(FaqType faqType) {
        //校验
        if (StringUtils.isNotBlank(faqType.getCode())) {
            long count = faqtypeRepository.countByCodeAndIdNot(faqType.getCode(), faqType.getId());
            if (count > 0) {
                throw new ControllerException("编码已存在");
            }
        } else {
            throw new ControllerException("请输入编码");
        }
        super.update(faqType);
    }

    @Override
    public void del(String[] ids) {
        for (String id : ids) {
            //问题引用校验
            long count = faqRepository.countByFaqTypeId(id);
            if (count > 0) {
                FaqType faqType = faqtypeRepository.findOne(id);
                throw new ControllerException("'" + faqType.getName() + "'分类已被引用，请先删除其常见问题子集");
            }
            faqtypeRepository.delete(id);
        }
    }
}
